package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.module.excitation.fragment.DianZanWallFragment;
import com.lishe.lishe.lishe.module.excitation.fragment.MineExcitationFragment;
import com.lishe.lishe.lishe.module.excitation.fragment.SendDianZanFragment;
import com.lishe.lishe.lishe.utils.StatusBarUtil;
import com.lishe.lishe.lishe.widget.CircleImageView;
import com.lishe.lishe.lishe.widget.MyFragmentTabHost;

import java.util.LinkedList;

/**
 * Created by shino on 2017/10/20.
 * 激励模块首页
 */

public class ExcitationActivity0 extends BaseActivity implements View.OnClickListener {

    private MyFragmentTabHost tabHost;
    private CircleImageView sendDianZan;
    private LinkedList<ImageView> lisTabIcons = new LinkedList<>();
    private LinkedList<TextView> lisTabTexts = new LinkedList<>();
    private int[] normolIcns = {R.mipmap.dianzan_gray, R.mipmap.me_icon, R.mipmap.normal_mine};
    private int tabPage = 0;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, ExcitationActivity0.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_excitation0);
        initView();
    }

    private void initView() {
        sendDianZan = findViewById(R.id.send_dianzan);
        tabHost = findViewById(R.id.tab_host);
        tabHost.setup(ExcitationActivity0.this, getSupportFragmentManager(), android.R.id.tabcontent);
        tabHost.addTab(tabHost.newTabSpec("dianzan").setIndicator(initIndicator(R.mipmap.dianzan_gray, "点赞墙")), DianZanWallFragment.class, null);
//        tabHost.addTab(tabHost.newTabSpec("dianzan").setIndicator(initIndicator(R.mipmap.dianzan_gray, "点赞墙")), GiveLikeWallFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("faqidainzan").setIndicator(initIndicatorSendDianzan(R.mipmap.giftnote_icon, "发起点赞")), SendDianZanFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("myexcition").setIndicator(initIndicator(R.mipmap.normal_mine, "我的激励")), MineExcitationFragment.class, null);

        setListener();
    }

    private void setListener() {
        //拦截点击tab事件(发起点赞)
        tabHost.getTabWidget().getChildTabViewAt(1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tabHost.setCurrentTab(1);
            }
        });
        sendDianZan.setOnClickListener(this);
        tabHost.setCurrentTab(tabPage); // 设置第一次打开时默认显示的标签，
        lisTabIcons.get(tabPage).setImageResource(R.mipmap.dianzan_click);
        lisTabTexts.get(tabPage).setTextColor(Color.parseColor("#ff4138"));
        tabHost.setOnTabChangedListener(tabChangelistenre);
    }

    /**
     * table切换监听
     */
    TabHost.OnTabChangeListener tabChangelistenre = new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String s) {

            lisTabIcons.get(tabPage).setImageResource(normolIcns[tabPage]);
            lisTabTexts.get(tabPage).setTextColor(getResources().getColor(R.color.gray_6));
            switch (s) {
                case "dianzan":
                    tabPage = 0;
                    lisTabIcons.get(0).setImageResource(R.mipmap.dianzan_click);
                    lisTabTexts.get(0).setTextColor(Color.parseColor("#ff4138"));
                    break;
                case "myexcition":
                    tabPage = 2;
                    lisTabIcons.get(2).setImageResource(R.mipmap.sel_mine);
                    lisTabTexts.get(2).setTextColor(Color.parseColor("#ff4138"));
                    break;
            }
        }
    };

    /**
     * 沉浸式颜色值修改
     */
    private void changeStatusBarColor(int color) {
        if (StatusBarUtil.setLightMode(this)) {
            StatusBarUtil.setColorNoTranslucent(this, color);
        } else {
            StatusBarUtil.setColor(this, color);
        }
    }

    /**
     * 初始化tab栏
     *
     * @return
     */

    private View initIndicator(int imgId, String tabText) {
        View view = getLayoutInflater().inflate(R.layout.item_main_tab_host_view, null);
        ImageView imgTabIcon = view.findViewById(R.id.tab_icon);
        TextView txtTabText = view.findViewById(R.id.tab_text);
        imgTabIcon.setImageResource(imgId);
        txtTabText.setText(tabText);
        lisTabIcons.add(imgTabIcon);
        lisTabTexts.add(txtTabText);
        return view;
    }

    private View initIndicatorSendDianzan(int imgId, String tabText) {
        View view = getLayoutInflater().inflate(R.layout.item_main_tab_host_view, null);
        ImageView imgTabIcon = view.findViewById(R.id.tab_icon);
        TextView txtTabText = view.findViewById(R.id.tab_text);
        imgTabIcon.setImageResource(imgId);
        txtTabText.setText(tabText);
        lisTabIcons.add(imgTabIcon);
        lisTabTexts.add(txtTabText);
        view.setVisibility(View.INVISIBLE);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_dianzan:
                jumpFragmentAdd(R.id.container, SendDianZanFragment.newInstance());
                break;
        }
    }

}
